/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.entity.GlobalConfig;
import com.hboxs.asl.service.GlobalConfigService;
import com.hboxs.asl.util.ListUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * Controller - 全局设定
 */
@Controller("adminGlobalConfigController")
@RequestMapping("/asl-admin/global_config")
public class GlobalConfigController extends BaseController {

    @Resource(name = "globalConfigServiceImpl")
    private GlobalConfigService globalConfigService;

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(ModelMap model) {
        GlobalConfig globalConfig = globalConfigService.getUniqueness();
        String tag = ListUtils.list2String(globalConfig.getRegisterTag());
        model.addAttribute("tag", tag);
        model.addAttribute("globalConfig", globalConfig);
        return "/admin/global_config/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(GlobalConfig globalConfig, String tag, RedirectAttributes redirectAttributes, ModelMap model) {
        if (!isValid(globalConfig)) {
            return ERROR_VIEW;
        }
        globalConfig.setRegisterTag(ListUtils.string2List(tag));
        globalConfigService.update(globalConfig);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:edit.htm";
    }

}
